//
//  MasterViewController.m
//  分屏显示4
//
//  Created by 奚旺 on 15/12/8.
//  Copyright © 2015年 奚旺. All rights reserved.
//

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "normalTableViewCell.h"
#import "XWMain.h"
#import "XWAFNetworkTool.h"

@interface MasterViewController ()
@property(nonatomic,strong)NSMutableArray * cellArray;

@property NSMutableArray *objects;
@end

@implementation MasterViewController
-(void)handleData
{
    
    self.cellArray = [NSMutableArray array];
    [XWAFNetworkTool getUrl:@"http://api.guju.com.cn/v2/strategy/strategyList?userId=&count=50&start=0&" body:nil response:XWJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray * array = [resposeObject objectForKey:@"strategyList"];
        for (NSDictionary * dic in array) {
            XWMain * model = [[XWMain alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.cellArray addObject:model];
            
        }
        
        
        [self.tableView reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
//    [self.tableView registerClass:[normalTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

- (void)viewWillAppear:(BOOL)animated {
    self.clearsSelectionOnViewWillAppear = self.splitViewController.isCollapsed;
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender {
    if (!self.objects) {
        self.objects = [[NSMutableArray alloc] init];
    }
    [self.objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        XWMain *object = self.cellArray[indexPath.row];
        DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
        [controller setDetailItem:object];
        controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
        controller.navigationItem.leftItemsSupplementBackButton = YES;
    }
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XWMain * main = self.cellArray[indexPath.row];
    normalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.normal = main;
    
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    
////    NSLog(@"array~~~~~~~~~~~~~~~~~~~%ld",self.cellArray.count);
////    XWMain * normal = self.cellArray[indexPath.row];
////    
////    
////    
////    DetailViewController *controller = (DetailViewController *)[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"123"];
////    //    DetailViewController * controller = [[DetailViewController alloc]init];
////    NSLog(@"%@", controller);
////    [controller setDetailItem:normal];
////    
////    
////    controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
////    controller.navigationItem.leftItemsSupplementBackButton = YES;
//
//    
//    
//}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

@end
